package com.qding.park.adapter.ark.adapter.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qding.park.adapter.ark.adapter.config.ARKConfig;
import com.qding.park.adapter.ark.adapter.constant.MethodConstant;
import com.qding.park.adapter.ark.adapter.entity.ResponseMsg;
import com.qding.park.adapter.ark.adapter.enums.InvitationEventEnum;
import com.qding.park.adapter.ark.adapter.enums.RespCodeEnums;
import com.qding.park.adapter.ark.adapter.util.Helper;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.adapter.AdapterException;
import com.qding.park.common.core.exceptions.system.NetWorkException;
import com.qding.park.common.core.network.HttpUtil;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.target.common.adapter.InviteAdapter;
import com.qding.park.target.common.entity.AdInviteOrder;

/**
 * @ClassName InviteAdapterImpl
 * @Description TODO(邀请接口实现--方舟实现)
 * @author LeiYang
 * @Date 2017年5月9日 上午10:07:00
 * @version 1.0.0
 */
@Service("inviteAdapter_13")
public class InviteAdapterImpl implements InviteAdapter {

    private static final Logger _log = LoggerFactory.getLogger(InviteAdapterImpl.class);
    
    /*
     * (非 Javadoc) Description:创建邀请订单
     * @see com.qding.park.target.common.adapter.InviteAdapter#createInvite(com.qding.park.target.common.entity.
     * AdInviteOrder)
     */
    @Override
    public void createInvite(AdInviteOrder apInviteOrder) throws AdapterException {
        String url = ARKConfig.getAdapterUrl();
        String urlMethod=Helper.createUrlAddress(url, MethodConstant.INVITATIONCAR);
        String orderId = apInviteOrder.getOrderId();
        String parkId = apInviteOrder.getParkCode();
        String carNo = apInviteOrder.getCarNum();
        String personId = apInviteOrder.getUserId();
        String invitationEvent = InvitationEventEnum.INVITE.getValue();
        String startTime = DateUtils.formatDate(apInviteOrder.getStartTime(), DateUtils.DATE_FORMAT_DATETIME);
        String endTime = DateUtils.formatDate(apInviteOrder.getEndTime(), DateUtils.DATE_FORMAT_DATETIME);

        Map<String, String> params = new HashMap<String, String>();
        params.put("orderId", orderId);
        params.put("parkId", parkId);
        params.put("carNo", carNo);
        params.put("personId", personId);
        params.put("invitationEvent", invitationEvent);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        try {
            String result = HttpUtil.doPost(urlMethod, params);
            ResponseMsg response = Helper.analysisResponse(result);

            
            if (!RespCodeEnums.SUCCESS.getValue().equals(response.getCode())) {
                // 失败
                ExceptionMessage exceptionMessage = ExceptionMessage.createExceptionMessage(
                        AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getDesc(), response.getMsg(),
                        apInviteOrder.toString());

                throw new AdapterException(AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getId(), null,
                        exceptionMessage);
            }
        } catch (NetWorkException | IOException e) {
            _log.error(e.getMessage(), e);
            ExceptionMessage exceptionMessage = ExceptionMessage.createExceptionMessage(
                    AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getDesc(), "方舟接口网络暖异常", apInviteOrder.toString());
            throw new AdapterException(AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getId(), e, exceptionMessage);
        }

    }

    /* (非 Javadoc)
     * Description:取消邀请
     * @see com.qding.park.target.common.adapter.InviteAdapter#cancelInvite(com.qding.park.target.common.entity.AdInviteOrder)
     */
    @Override
    public void cancelInvite(AdInviteOrder apInviteOrder) throws AdapterException {
        String url = ARKConfig.getAdapterUrl();
        String urlMethod=Helper.createUrlAddress(url, MethodConstant.INVITATIONCAR);
        String orderId = apInviteOrder.getOrderId();
        String parkId = apInviteOrder.getParkCode();
        String carNo = apInviteOrder.getCarNum();
        String personId = apInviteOrder.getUserId();
        String invitationEvent = InvitationEventEnum.CANCEL.getValue();
//        String startTime = DateUtils.formatDate(apInviteOrder.getStartTime(), DateUtils.DATE_FORMAT_DATETIME);
//        String endTime = DateUtils.formatDate(apInviteOrder.getEndTime(), DateUtils.DATE_FORMAT_DATETIME);

        Map<String, String> params = new HashMap<String, String>();
        params.put("orderId", orderId);
//        params.put("parkId", parkId);
//        params.put("carNo", carNo);
        params.put("personId", personId);
        params.put("invitationEvent", invitationEvent);
//        params.put("startTime", startTime);
//        params.put("endTime", endTime);
        try {
            String result = HttpUtil.doPost(urlMethod, params);
            ResponseMsg response = Helper.analysisResponse(result);

            
            if (!RespCodeEnums.SUCCESS.getValue().equals(response.getCode())) {
                // 失败
                ExceptionMessage exceptionMessage = ExceptionMessage.createExceptionMessage(
                        AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getDesc(), response.getMsg(),
                        apInviteOrder.toString());

                throw new AdapterException(AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getId(), null,
                        exceptionMessage);
            }
        } catch (NetWorkException | IOException e) {
            _log.error(e.getMessage(), e);
            ExceptionMessage exceptionMessage = ExceptionMessage.createExceptionMessage(
                    AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getDesc(), "方舟接口网络暖异常", apInviteOrder.toString());
            throw new AdapterException(AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getId(), e, exceptionMessage);
        }
    }

}
